#include <stdlib.h>
#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "oled_GME12864.h"
#include "oled_bmp.h"
#include "oled_gui.h"
#include"lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "lwip/sockets.h"
#include "WifiSta.h"
#include "BH1750.h"
#include "Temp.h"
#include "Pwm.h"
#include "Led.h"
#define SSID "Nice"
#define Key "12312312"
#define TCP_IP "192.168.101.231"
#define TCP_PORT 5552
#define SENDMSG_HEAD "A"
int socketfd;
char sendBuf[256] = {0};
char temp_str[20] = {0},humi_str[20] = {0},light_str[20] = {0};
float tem,humi,light;
int auto_flag = 1;
int w;
float htem = 29,llight = 150;
void TASK1(void);
void TASK2(void);
void TASK3(void);
void Oled_Init();
void Show_Text(u8 x,u8 y,u8 hsize,u8 *str,u8 mode);
void TEST_Menu_Env(float temperature, float humidity,float intensi,int wifi,int mode);

static void OledSample(){
    Oled_Init();
    printf("=====Envi DEMO=====\n");
    osThreadAttr_t attr;
    attr.name = "Task1";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 10;
    attr.priority = osPriorityNormal;
    osThreadNew(TASK1,NULL,&attr);
    attr.name = "Task2";
    osThreadNew(TASK2,NULL,&attr);
    attr.name = "TASK3";
    osThreadNew(TASK3,NULL,&attr);
}
void TASK1(void){
    initI2C();
    InitSHT30();
    Led_Init();
    Led_Off();
    pwm_Init();
    pwm_Set(0,4000);
    BH1750_Init();
    TEST_Menu_Env(tem,humi,light,0,auto_flag);
    sleep(5);
    while (1)
    {
        SHT3X_ReadMeasurementBuffer(&tem,&humi);
        BH1750_GetData(&light);
        printf("aaa=%f\n",light);
        if(auto_flag == 1){
            if(tem > htem) pwm_Set(70,4000);
            else pwm_Set(0,4000);
            if(light < llight) Led_On();
            else Led_Off();
        }
        TEST_Menu_Env(tem,humi,light,w,auto_flag);
        sprintf(temp_str, "%f", tem);
        sprintf(humi_str,"%f",humi);
        sprintf(light_str,"%f",light);
        OLED_Display();
        usleep(100000);
    }
}
void TASK2(void){
struct sockaddr_in send_addr={0};
    //WIFI 连接
    ip4_addr_t ip  = WifiConnect(SSID,Key);
    sleep(2);

    //创建socket
    socketfd = socket(AF_INET,SOCK_STREAM,0);
    if(socketfd == -1)
    {
        printf("socket create failed\n");
        return -1;
    }
    printf("socket create ok\n");

    send_addr.sin_family = AF_INET;
    send_addr.sin_addr.s_addr = inet_addr(TCP_IP);
    send_addr.sin_port = htons(TCP_PORT);
    //连接服务端
    int ret = connect(socketfd,(struct sockaddr *)&send_addr,sizeof(send_addr));
    if (ret != 0)
    {
        printf("socket connect failed is %d\n",ret);
        closesocket(socketfd);
        return -1;
    }
    printf("socket connect ok\n");
    w = 1;
    //连接服务器后发送和接收数据
    char str[20] = {0};
    int count = 0;
    while (1)
    {
      memset(sendBuf,0,sizeof(sendBuf));
      if(count < 1){//发送两次。每次延时2s
        strcpy(sendBuf, SENDMSG_HEAD);  
        sprintf(str, "%d",ip);
        strcat(sendBuf,str);
        count ++;
      }else{
        strcpy(sendBuf, SENDMSG_HEAD); 
        strcat(sendBuf,temp_str);
        strcat(sendBuf,"-");
        strcat(sendBuf,humi_str);
        strcat(sendBuf,"-");
        strcat(sendBuf,light_str);
      }
      
      ret = send(socketfd,sendBuf,strlen(sendBuf)+1,0);
      if (ret == -1)
        {
            printf("socket send failed is %d\n",ret);
            break;
        }
        printf("socket send ok\n");
        sleep(2);
    }
    closesocket(socketfd);
    return 0;
}
void TASK3(void){
    char recvbuf [512] = {0},fan_str[20] = {0};
    unsigned short duty;
    int ret = 0;
    sleep(8);
    while(1){
        memset_s(recvbuf, sizeof(recvbuf), 0, sizeof(recvbuf));
        if ((ret = recv(socketfd, recvbuf, sizeof(recvbuf), 0)) == -1)
        {
            printf("recv error \r\n");
        }
        else
        {	
            printf("recv :%s\r\n", recvbuf);
            if(strcmp(recvbuf,"ON") == 0) {
                auto_flag = 0;
                Led_On();
            }else if (strcmp(recvbuf,"OFF") == 0)
            {
                auto_flag = 0;
                Led_Off();
            }
            if(strstr(recvbuf,"Fan") != NULL){
                auto_flag = 0;
                strncpy(fan_str,recvbuf,2);
                printf("aa %s\n",fan_str);
                sscanf(fan_str, "%d", &duty);
                printf("aa %d\n",duty);
                pwm_Set((unsigned short)duty,4000);
            }
        }
        usleep(100);
    }
}
void Oled_Init(){
    printf("====Oled Init====\n");
    OLED_Init_GPIO();
    OLED_Init();
    osDelay(100);
    OLED_Clear(0);
}

void TEST_Menu_Env(float temperature, float humidity,float intensi,int wifi,int mode)
{
	OLED_Clear(0);
    char buf[15] = {0};
	float temp = temperature;
	float humi = humidity;

	//printf(">>>> show oled: temp:%d, humi:%d\n", temp, humi);
	GUI_DrawLine(0, 10, WIDTH-1, 10,1);
	GUI_DrawLine(WIDTH/2-1,11,WIDTH/2-1,HEIGHT-1,1);
	GUI_DrawLine(WIDTH/2-1,10+(HEIGHT-10)/2-1,WIDTH-1,10+(HEIGHT-10)/2-1,1);
	if(wifi == 0)
	{
		GUI_ShowString(0,1,"no-wifi-",8,1);
	}
	else
	{
		GUI_ShowString(0,1,"--wifi--",8,1);
	}

	if(mode == 0)
	{
		GUI_ShowString(78,1,"curtain-",8,1);
	}
	else if(mode == 1)
	{
		GUI_ShowString(78,1,"auto",8,1);
	}
	else if(mode == 3)
	{
		GUI_ShowString(78,1,"watering",8,1);
	}
	else if(mode == 4)
	{
		GUI_ShowString(78,1,"faning--",8,1);
	}
	else
	{
		if(wifi == 0)
		{
			GUI_ShowString(78,1,"linking-",8,1);
		}
		else{
			GUI_ShowString(78,1,"working-",8,1);	
		}
	}
	//GUI_ShowString(14,HEIGHT-1-10,"Cloudy",8,1);
    GUI_ShowString(6,20,"Light",8,1);
    memset(buf, 0x00, sizeof(buf));
    sprintf(buf, "%0.1f", intensi);
    strcat(buf,"lu");
    GUI_ShowString(2,HEIGHT-1-22,buf,16,1);
	GUI_ShowString(WIDTH/2-1+2,13,"TEMP",8,1);
	GUI_DrawCircle(WIDTH-1-19, 25, 1,2);
	GUI_ShowString(WIDTH-1-14,20,"C",16,1);
    memset(buf, 0x00, sizeof(buf));
    sprintf(buf, "%0.1f", temp);
	GUI_ShowString(WIDTH/2-1+9,20,buf,16,1);
	GUI_ShowString(WIDTH/2-1+2,39,"Humi",8,1);
	memset(buf, 0x00, sizeof(buf));
    sprintf(buf, "%0.1f", humi);
	GUI_ShowString(WIDTH/2-1+9,46,buf,16,1);
	// GUI_DrawBMP(6,16,51,32, BMP5, 1);
	OLED_Display();
}
APP_FEATURE_INIT(OledSample);
